﻿using DotNet.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using DotNet.Extensions;
using System.Text;
using System.Threading.Tasks;

namespace LongAndLatConvert
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdateRoute();


            Console.ReadLine();
        }

        static void UpdateRoute()
        {
            var httpClient = new HttpClient();
            var db = new Database();
            var dt = db.ExecuteDataTable("select routeid,segmentid,oldxljw from hxxljwd where newxljw is null");
            foreach (DataRow row in dt.Rows)
            {
                var routeid = row["routeid"].ToString();
                var oldxljw = row["oldxljw"].ToString();
                var segmentid = row["segmentid"].ToString();
                if (string.IsNullOrEmpty(oldxljw))
                {
                    continue;
                }
                var sz = oldxljw.Split(',');

                Console.WriteLine($"开始 站点={routeid},segmentid={segmentid}");

                int index = 0;
                int count = sz.Length - 1;
                var sb = new StringBuilder();
                while (index <= count)
                {
                  
                    var longu = sz[index];
                    var latu = sz[index + 1];

                    var url = "http://125.72.62.19:8885/decode/get84to02.jsp?latlon=" + $"{longu},{latu}";
                    var result = httpClient.GetStringAsync(url).Result;
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.Load(new MemoryStream(Encoding.UTF8.GetBytes(result)));
                    var selectSingleNode = doc.SelectSingleNode("result/item/lonlat");
                    if (selectSingleNode != null)
                    {
                        var t = selectSingleNode.InnerText;
                        var array = t.Split(',');
                        var newlongitude = array[0];
                        var newlatitude = array[1];
                        sb.Append($"{newlongitude},{newlatitude},");
                    }
                    
                    //Console.WriteLine();
                    index += 2;
                }
                var xljw = sb.ToString().TrimEnd(',');
                var sql = "update hxxljwd set newxljw=@newxljw where routeid=@routeid";
                db.Execute(sql,new { newxljw= xljw, routeid= routeid });
                Console.WriteLine($"更新 站点={routeid},segmentid={segmentid}");
            }
            
            Console.WriteLine("线路经纬度信息更新完成");
        }

        static void UpdateStation()
        {
            var httpClient = new HttpClient();
            var db = new Database();
            var dt = db.ExecuteDataTable("select stationid, stationname, oldlongitude, oldlatitude from hxzdjwd  where oldlongitude>0");
            //newlongitude, newlatitude
            foreach (DataRow row in dt.Rows)
            {
                var stationid = row["stationid"].ToString();
                var stationname = row["stationname"].ToString();
                var oldlongitude = row["oldlongitude"].ToString();
                var oldlatitude = row["oldlatitude"].ToString();
                var url = "http://125.72.62.19:8885/decode/get84to02.jsp?latlon=" + $"{oldlongitude},{oldlatitude}";
                var result = httpClient.GetStringAsync(url).Result;
                /*
                 * <result>
                        <item count="1">
                            <lonlat id="0">0.01549,0.00272</lonlat>
                        </item>
                    </result>
                 */
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(new MemoryStream(Encoding.UTF8.GetBytes(result)));
                var selectSingleNode = doc.SelectSingleNode("result/item/lonlat");
                if (selectSingleNode != null)
                {
                    var t = selectSingleNode.InnerText;
                    var array = t.Split(',');
                    var newlongitude = array[0];
                    var newlatitude = array[1];
                    db.Execute($"update hxzdjwd set newlongitude='{newlongitude}', newlatitude='{newlatitude}' where stationid='{stationid}'");
                    Console.WriteLine($"更新 站点={stationname},坐标={selectSingleNode.InnerText}");
                    Console.WriteLine();
                }
            }
            Console.WriteLine("站点经纬度信息更新完成");
        }
    }
}
